//
//  BaseViewController.m
//  Weibo
//
//  Created by taoshanchang on 15/1/21.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "BaseViewController.h"
#import "UIFaactory.h"
#import "CONSTS.h"
#import "MBProgressHUD.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        self.isCanBack = YES;
    }
    return self;
}

-(AppDelegate *)appDelegate{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    return appDelegate;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *viewControllers = self.navigationController.viewControllers;
    if(viewControllers.count > 1 && self.isCanBack){
        UIButton *button = [UIFaactory createButton:@"navigationbar_back" hightlighted:@"navigationbar_back_highlighted"];
        button.showsTouchWhenHighlighted = YES;
        button.frame = CGRectMake(0, 0, 24, 24);
        [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.leftBarButtonItem = barButton;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI

-(void)showLoading:(BOOL)show{
    
    if(_loadView == nil){
    
        _loadView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenWidth/2-80, ScreenHeight, 20)];
    
    }



}

-(void)showHUD{
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.dimBackground = YES;

}

-(void)hideHUD{
    [self.hud hide:YES];
}

-(void)showHUDComplete:(NSString *)title{
    self.hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark"]];
    self.hud.mode = MBProgressHUDModeCustomView;
    if(title.length > 0){
        self.hud.labelText = title;
    }
    [self.hud hide:YES afterDelay:1];
}

-(void)setTitle:(NSString *)title{
    [super setTitle:title];
    //改用自定义的Label
    //UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    UILabel *titleLabel = [UIFaactory createLabel:kNavigationBarTitleLabel];
    //titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    titleLabel.backgroundColor  = [UIColor clearColor];
    titleLabel.text = title;
    [titleLabel sizeToFit];
    
    self.navigationItem.titleView = titleLabel;

}

#pragma - mark action
-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
